# coding: utf8

import os
import logging
import time
import re
import traceback
import requests_html
from multiprocessing.pool import ThreadPool


"""天津大学
微电子学院
"""

logging.basicConfig(level=logging.INFO)

session = requests_html.HTMLSession()
pool = ThreadPool(os.cpu_count() * 2)  # 要使用 hanlp, 线程数设为1
host = "http://sme.tju.edu.cn"


add_breakline = re.compile(r'(\s+([一二三四五六七])|([123456789][. ]+)\s*\S+)')  # 用于区分主目录
reduce_breakline = re.compile(r"[ \n]+")  # 避免与主目录混淆
reduce_breakline2 = re.compile(r"\n{2,}")
reduce_dollar = re.compile(r'(\$\$\s*){2,}')
reduce_dollar2 = re.compile(r'(\$\s*)+')
addr_ptn = re.compile(r'(?:通信地址|通讯地址|办公地址|Address)[:：]([ \S]+)')
tel_ptn = re.compile(r'(?:办公电话|电话|联系方式|Tel|tel)[:：]([0-9+\- ]+)')
email_ptn = re.compile(r'(?:电子邮箱|电子信箱|E-mail|Email|email|e-mail|邮箱地址|邮\s*箱)[:：](\S+@[.\S]+)')
ri_ptn = re.compile(r'研究(?:方向|领域|重点)(?:为|包括)*[:：\n]*([ \n\S]+?(?:\n{4,}|。))')
pos_ptn = re.compile(r"(教授|副教授|助理教授|研究员|副研究员|博士导师|硕士导师|博士生研究生|硕士生导师|IEEE Fellow|IEEE\s*会员|博士|讲师|教师)")
edu_ptn = re.compile(r"(?:考入|攻读|进修|毕业于|学位|博士后|访问|工程师)")
split_ptn = re.compile(r'([;；。\n])')


def teacher_list():
    try:
        r = session.get("http://sme.tju.edu.cn/szduiwu/szgaikuang/")
        if r.status_code == 200:
            lis = r.html.xpath("//div[@id='rencaipeiyang-daoshi']/ul/li")

            teachers = []
            for li in lis:
                url = list(li.xpath('//a[1]')[0].absolute_links)[0]
                name = li.xpath("//span[1]")[0].full_text.strip()

                logging.info("name: %s, url: %s", name, url)
                teachers.append((name, url))
            pool.imap_unordered(teacher_detail, teachers)
        else:
            logging.warning("wrong status code: %s", r.status_code)
            r.raise_for_status()
    except Exception as e:
        traceback.print_exc()
        raise e


def teacher_detail(payload):
    name, url = payload
    logging.info("loading %s ...", name)

    try:
        r = session.get(url)
        if r.status_code == 200:
            avatar = r.html.xpath("//div[@id='shizixiangxi']//img[1]/@src")
            avatar = r.html.base_url + avatar[0] if avatar else ''

            # content = r.html.xpath("//div[@id='shizixiangxi']")[0].full_text
            # content = r.html.xpath("//div[@id='shizixiangxi']//*[not(self::script or self::style)]/text()")  # exclude js and style sheet
            ps = r.html.xpath("//div[@id='shizixiangxi']//*[self::p or self::td]")
            content = "$$".join([p.full_text for p in ps])
            content = content.replace('$$', '\n')
            content = add_breakline.sub(r"\n\n\n\n\1", content)
            content = content.replace(" ", "").strip()

            # logging.info("name: %s, avatar: %s, content: %s", name, avatar, content)

            addr, tel, email, ri = [ptn.search(content) for ptn in (addr_ptn, tel_ptn, email_ptn, ri_ptn)]
            addr, tel, email, ri = [(match.group(1).strip() if match else '') for match in (addr, tel, email, ri)]

            pos = set(pos_ptn.findall(content))
            if '教授' in pos:
                pos.discard('助理教授')
                pos.discard('副教授')
                pos.discard('研究员')
                pos.discard('副研究员')
                pos.discard('讲师')
                pos.discard('教师')
            if '副教授' in pos:
                pos.discard('研究员')
                pos.discard('副研究员')
                pos.discard('讲师')
                pos.discard('教师')
            if '研究员' in pos:
                pos.discard('副研究员')

            pos = ','.join(list(pos))

            sentences = split_ptn.split(content)

            edu_sent = []
            for sent in sentences:
                if edu_ptn.search(sent):
                    edu_sent.append(sent)
            edu = "。".join(edu_sent)

            logging.info("")
            logging.info("")
            logging.info("name: %s, pos: %s, tel: %s, email: %s, addr: %s, edu: %s, ri: %s， avatar: %s", name, pos, tel, email, addr, edu, ri, avatar)

            pass
        else:
            logging.warning("wrong status code: %s", r.status_code)
            r.raise_for_status()
    except Exception as e:
        traceback.print_exc()
        raise e


if __name__ == '__main__':
    teacher_list()

    pool.close()
    pool.join()

    session.close()
    pass


